package com.dfqy.service;

import com.dfqy.dao.EventDao;
import com.dfqy.pojo.Event;
import com.dfqy.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author : atyushu
 * Package : com.dfqy.service
 * ClassName : EventService
 * Create : 2023/8/1 - 19:23
 * Version : v1.0
 * Description : The class of service for Event.
 */
@Service
@Cacheable(keyGenerator = "keyg", cacheNames = "event")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
public class EventService {

    @Autowired
    private EventDao eventDao;

    @CacheEvict(allEntries = true, cacheNames = "event")
    public int insert(Event event) {
        return eventDao.insert(event);
    }

    @CacheEvict(allEntries = true, cacheNames = "event")
    public int delete(long... id) {
        return eventDao.delete(id);
    }

    @CacheEvict(allEntries = true, cacheNames = "event")
    public int update(Event event) {
        return eventDao.update(event);
    }

    public Event getById(long id) {
        return eventDao.getById(id);
    }

    public Page<Event> page(int page, int limit, String poster, String stype, String title) {
        return new Page<>(eventDao.page(page, limit, poster, stype, title), page, eventDao.countByLike(poster, stype, title), limit);
    }

    public List<Event> getAll() {
        return eventDao.getAll();
    }

    public List<Event> search(String addr, String stype, String orderBy) {
        return eventDao.search(addr, stype, orderBy);
    }

    @CacheEvict(allEntries = true, cacheNames = "event")
    public int updateVisitnums(long id) {
        return eventDao.updateVisitnums(id);
    }

    public Page<Event> getTitle(int page, int limit) {
        return new Page<>(eventDao.getTitle(page, limit), page, eventDao.countTotal(), limit);
    }
}
